package editor;

import javax.swing.*;
import editor.Menu.OGGETTI;
import editor.Menu.STRUMENTI;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import logic.Coords2D;

/**
 * 
 * @author Massimiliano Palermo
 *
 */
public class GridPanel extends JPanel implements MouseListener,
												 MouseMotionListener,
												 KeyListener{
	
	private static final long serialVersionUID = 2504101754L; 
    private boolean btnLeftPressed,btnRightPressed;
    private int startX,startY;
    private Coords2D initPoint;
    private Coords2D endPoint;
    
	public GridPanel() {   
        this.setFocusable(true);
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        this.addKeyListener(this);
        this.setFocusable(true);
        this.setVisible(true);
        initPoint=new Coords2D(0,0);
         endPoint=new Coords2D(0,0);
    }
	
	public void clearGrid()
	{
		for (int i=0;i<Grid.WIDTH;i++)
		{
			for(int j=0;j<Grid.HEIGHT;j++)
				Grid.setCell(i, j, OGGETTI.EMPTY);
		}
		repaint();
	}
    
	/**
     * Disegno la griglia 
     */
	@Override
    public void paintComponent(Graphics g)
	{
		for(int i=0;i<Grid.WIDTH;i++)
			for (int j=0;j<Grid.HEIGHT;j++)
			{
				switch(Grid.getCell(i, j))
				{
				case EXTWALL   :{g.setColor(Grid.wallColor);break;}
				case PILL      :{g.setColor(Grid.pillColor);break;}
				case STARTPOINT:{g.setColor(Grid.startColor);break;}
				case RUBYRED   :{g.setColor(Grid.rubyRedColor);break;}
				case RUBYGREEN :{g.setColor(Grid.rubyGreenColor);break;}
				case RUBYBLUE  :{g.setColor(Grid.rubyBlueColor);break;}
				case EMPTY     :{g.setColor(Grid.emptyColor);break;}
				}
				g.fillRect(i*Editor.cellW+1, j*Editor.cellH+1, Editor.cellW-1,Editor.cellH-1);
			}
		
			g.setColor(Color.gray.brighter());
			for (int i=0;i<Grid.WIDTH*Editor.cellW;i=i+Editor.cellW)
	        	g.drawLine(i, 0, i, this.getSize().height);
			for (int i=0;i<Grid.HEIGHT*Editor.cellH;i=i+Editor.cellH)
	        	g.drawLine(0, i, this.getSize().width, i);
			
			if (Menu.currTool==STRUMENTI.LINE)
			{
				System.out.println("TOOL: line");
				int x1=(int)initPoint.getX();
				int y1=(int)initPoint.getY();
				int x2=(int) endPoint.getX();
				int y2=(int) endPoint.getY();
				
				g.setColor(new Color(100,100,100));
				g.drawLine(x1,y1,x2,y2);
			}
			else if (Menu.currTool==STRUMENTI.RECT)
			{
				System.out.println("TOOL: line");
				int x1=(int)initPoint.getX();
				int y1=(int)initPoint.getY();
				int x2=(int) endPoint.getX();
				int y2=(int) endPoint.getY();
				
				g.setColor(new Color(100,100,100));
				g.drawRect(x1, y1, x2-x1, y2-y1);
			}	
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		int x,y;
		
		btnLeftPressed=false;
		btnRightPressed=false;
		Editor.isDirty=true;
		
		if (e.getButton()==MouseEvent.BUTTON1) btnLeftPressed=true;
		if (e.getButton()==MouseEvent.BUTTON3) btnRightPressed=true;
		
		if (btnLeftPressed && btnRightPressed) 
		{
			btnLeftPressed=false;
			btnRightPressed=false;
		}
		
		if (btnLeftPressed)
		{
			startX=e.getX();
			startY=e.getY();
			x=(int)(startX/Editor.cellW);
			y=(int)(startY/Editor.cellH);
			
			if (Menu.currTool==STRUMENTI.CELL)
				Grid.setCell(x, y, Menu.currObject);
			else if (Menu.currTool==STRUMENTI.LINE)
			{
				initPoint = new Coords2D(x*Editor.cellW, y*Editor.cellH);
				endPoint  = new Coords2D(x*Editor.cellW, y*Editor.cellH);
			}
			else if (Menu.currTool==STRUMENTI.RECT)
			{
				initPoint = new Coords2D(x*Editor.cellW, y*Editor.cellH);
				endPoint  = new Coords2D(x*Editor.cellW, y*Editor.cellH);
			}
			e.getComponent().repaint();
			
		}
		
		if (btnRightPressed)
		{
			startX=e.getX();
			startY=e.getY();
			x=(int)(startX/Editor.cellW);
			y=(int)(startY/Editor.cellH);
			
			Grid.setCell(x, y, OGGETTI.EMPTY);
			e.getComponent().repaint();
		}
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		OGGETTI tmp = Menu.currObject;
		if (btnRightPressed) Menu.currObject = OGGETTI.EMPTY;
		
		if (Menu.currTool==STRUMENTI.RECT)
		{
			int x1 = (int) initPoint.getX()/Editor.cellW;
			int y1 = (int) initPoint.getY()/Editor.cellH;
			int x2 = (int)  endPoint.getX()/Editor.cellW;
			int y2 = (int)  endPoint.getY()/Editor.cellH;
			for (int i=x1;i<x2+1;i++)
			{
				Grid.setCell(i, y1);
				Grid.setCell(i, y2);
			}
			for (int i=y1;i<y2+1;i++)
			{
				Grid.setCell(x1, i);
				Grid.setCell(x2, i);
			}
		}
		if (Menu.currTool==STRUMENTI.LINE)
		{
			int x1 = (int) initPoint.getX()/Editor.cellW;
			int y1 = (int) initPoint.getY()/Editor.cellH;
			int x2 = (int)  endPoint.getX()/Editor.cellW;
			int y2 = (int)  endPoint.getY()/Editor.cellH;
			if(x1==x2)
			for (int i=y1;i<y2+1;i++)
				Grid.setCell(x1, i);
			if(y1==y2)
				for (int i=x1;i<x2+1;i++)
					Grid.setCell(i, y1);
		}
		if (btnRightPressed) Menu.currObject = tmp;
		btnLeftPressed=false;
		btnRightPressed=false;
		repaint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		int x=(int)(e.getX()/Editor.cellW);
		int y=(int)(e.getY()/Editor.cellW);
		OGGETTI t;
		
		if (btnRightPressed)
			t = OGGETTI.EMPTY;
		else
			t = Menu.currObject;

		if (Menu.currTool==STRUMENTI.CELL)
			Grid.setCell(x, y, t);
		else if (Menu.currTool==STRUMENTI.LINE)
		{
			int pX,pY;
			endPoint = new Coords2D(x*Editor.cellW, y*Editor.cellH);
			pX=(int)Math.abs(initPoint.getX()-endPoint.getX());
			pY=(int)Math.abs(initPoint.getY()-endPoint.getY());
			if (pX>pY)
				endPoint = new Coords2D(endPoint.getX(),initPoint.getY());
			else
				endPoint = new Coords2D(initPoint.getX(),endPoint.getY());
		}
		
		else if (Menu.currTool==STRUMENTI.RECT)
		{
			endPoint = new Coords2D(x*Editor.cellW, y*Editor.cellH);
			if(initPoint.getX()>endPoint.getX() || initPoint.getY()>endPoint.getY())
				endPoint = new Coords2D(initPoint);
		}
		e.getComponent().repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
		
	} 

	@Override
	public void keyReleased(KeyEvent e) {
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}
}